package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class TAOHAOYAAttr implements AdapterAttr {


    public static void main(String[] args) {
        String response = HttpRequest.get("https://api.taohaoyayy.com/v1/game/config-list?gameId=1").header("Authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NDEyNDU4NDYsImlhdCI6MTc0MDM4MTg0NiwibmJmIjoxNzQwMzgxODQ2LCJ1aWQiOjM3LCJ1c2VyVHlwZSI6M30.P5N7u5m7hDnpBQlXhK4XbSVRXMBnD1ENWe7JbvDibp4").execute().body();
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String response = HttpRequest.get("https://api.taohaoyayy.com/v1/game/list")
                .header("Authorization","Bearer "+salePartner.getTwoOther()).execute().body();
        JSONObject resJson = JSONObject.parseObject(response);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String response = HttpRequest.get("https://api.taohaoyayy.com/v1/game/option-list?gameId="+gameId)
                .header("Authorization","Bearer "+salePartner.getTwoOther()).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        JSONObject jsonObject1 = data.get(0);
        data = JSONObject.parseArray(jsonObject1.getString("child"), JSONObject.class);
        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setId(datum.getString("id"));
            interServer.setName(datum.getString("name"));
            String servers = datum.getString("child");
            if(StringUtils.isNotEmpty(servers)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject object : jsonObjects) {
                    InterServerRes interServer1 = new InterServerRes();
                    String id1 = object.getString("id");
                    String title1 = object.getString("name");
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                    String servers2 = object.getString("child");
                    if(StringUtils.isNotEmpty(servers2)) {
                        List<InterServerRes> interServerRes2 = new ArrayList<>();
                        List<JSONObject> jsonObjects2 = JSONObject.parseArray(servers2, JSONObject.class);
                        for (JSONObject object2 : jsonObjects2) {
                            InterServerRes interServer2 = new InterServerRes();
                            String id2 = object2.getString("id");
                            String title2 = object2.getString("name");
                            interServer2.setId(id2);
                            interServer2.setName(title2);
                            interServerRes2.add(interServer1);
                            interServer1.setInterServerRes(interServerRes2);
                        }
                    }
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String response = HttpRequest.get("https://api.taohaoyayy.com/v1/game/config-list?gameId="+gameId)
                .header("Authorization","Bearer "+salePartner.getTwoOther()).execute().body();
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("single")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("checkbox")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("type").equals("input")
                    ||d.getString("type").equals("text")||d.getString("type").equals("float2")
                    ||d.getString("type").contains("image")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setUuid(UUID.randomUUID().toString());
            interAttrRes.setId(object.getString("id"));
            if(StringUtils.isNotEmpty(object.getString("mappingField"))){
                interAttrRes.setName(object.getString("name")+"-"+object.getString("mappingField"));
            }else {
                interAttrRes.setName(object.getString("name"));
            }
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            String string = object.getString("options");
            if(StringUtils.isNotEmpty(string)) {
                JSONArray jsonArray = JSONArray.parseArray(string);
                for (Object o : jsonArray) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UUID.randomUUID().toString());
                    interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("key"));
                    interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("val"));
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}